package com.asdan.seededu.web.controller;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.asdan.seededu.entity.security.SubProjectStep;
import com.asdan.seededu.service.ProjectService;
import com.asdan.seededu.service.SilkroadProjectService;
import com.asdan.seededu.service.impl.ProjectServiceImpl;
import com.asdan.seededu.service.impl.SilkroadProjectServiceImpl;
import com.demo2do.core.web.interceptor.MenuSetting;

@Controller
@MenuSetting("menu-project")
@RequestMapping("projects/skilroad")
public class SkilRoadController {

	@RequestMapping(value="/updateStep",method=RequestMethod.POST)
	public String updateXxqr(String fpid,String spid,String stepid ,String stepName,String stepContext ,String stepillustrate ,@RequestParam("filename")String[] filename ,@RequestParam("attachs")MultipartFile[]  attachs,HttpServletRequest req,Model model) {
		SubProjectStep sps  = new SubProjectStep() ;
		sps.setStepid(Integer.parseInt(stepid)) ;
		sps.setStepName(stepName) ;
		sps.setStepContext(stepContext) ;
		sps.setStepillustrate(stepillustrate) ;
		String realPath = req.getSession().getServletContext().getRealPath("/attach/skilroad/xxqr") ;
		System.out.println(realPath);
			
		MultipartFile attach = attachs[0] ;
		if(!attach.isEmpty()) {
			sps.setAttachment1name(attach.getOriginalFilename()) ;
			sps.setAttachment1address(filename[0])  ;
			System.out.println(attach.getName()+"  "+attach.getOriginalFilename()+"  "+attach.getContentType());
				File f = new File(realPath+"/"+attach.getOriginalFilename()) ;
				try {
					FileUtils.copyInputStreamToFile(attach.getInputStream(), f) ;
						} catch (IOException e) {
							//	 TODO Auto-generated catch block
							e.printStackTrace();
				}
		}else{
			sps.setAttachment1name("") ;
			sps.setAttachment1address("")  ;
		}
		 attach = attachs[1] ;
		if(!attach.isEmpty()) {
			sps.setAttachment2name(attach.getOriginalFilename()) ;
			sps.setAttachment2address(filename[1])  ;
			System.out.println(attach.getName()+"  "+attach.getOriginalFilename()+"  "+attach.getContentType());
				File f = new File(realPath+"/"+attach.getOriginalFilename()) ;
				try {
					FileUtils.copyInputStreamToFile(attach.getInputStream(), f) ;
						} catch (IOException e) {
							//	 TODO Auto-generated catch block
							e.printStackTrace();
				}
			}else{
				sps.setAttachment2name("") ;
				sps.setAttachment2address("")  ;
			}
		 attach = attachs[2] ;
		if(!attach.isEmpty()) {
			sps.setAttachment3name(attach.getOriginalFilename()) ;
			sps.setAttachment3address(filename[2])  ;
			System.out.println(attach.getName()+"  "+attach.getOriginalFilename()+"  "+attach.getContentType());
				File f = new File(realPath+"/"+attach.getOriginalFilename()) ;
				try {
					FileUtils.copyInputStreamToFile(attach.getInputStream(), f) ;
						} catch (IOException e) {
							//	 TODO Auto-generated catch block
							e.printStackTrace();
				}
			}else{
				sps.setAttachment3name("") ;
				sps.setAttachment3address("")  ;
			}
		attach = attachs[3] ;
		if(!attach.isEmpty()) {
			sps.setAttachment4name(attach.getOriginalFilename()) ;
			sps.setAttachment4address(filename[3])  ;
			System.out.println(attach.getName()+"  "+attach.getOriginalFilename()+"  "+attach.getContentType());
				File f = new File(realPath+"/"+attach.getOriginalFilename()) ;
				try {
					FileUtils.copyInputStreamToFile(attach.getInputStream(), f) ;
						} catch (IOException e) {
							//	 TODO Auto-generated catch block
							e.printStackTrace();
				}
			}else{
				sps.setAttachment4name("") ;
				sps.setAttachment4address("")  ;
			}
		attach = attachs[4] ;
		if(!attach.isEmpty()) {
			sps.setAttachment5name(attach.getOriginalFilename()) ;
			sps.setAttachment5address(filename[4])  ;
			System.out.println(attach.getName()+"  "+attach.getOriginalFilename()+"  "+attach.getContentType());
				File f = new File(realPath+"/"+attach.getOriginalFilename()) ;
				try {
					FileUtils.copyInputStreamToFile(attach.getInputStream(), f) ;
						} catch (IOException e) {
							//	 TODO Auto-generated catch block
							e.printStackTrace();
				}
			}else{
				sps.setAttachment5name("") ;
				sps.setAttachment5address("")  ;
			}
		System.out.println(sps.toString());
		ProjectService ps = new ProjectServiceImpl() ;
		if(ps.updateSubProjectStep(sps) ){
			model.addAttribute("message", "丝绸之路确认信息编辑成功") ;
		}else{
			model.addAttribute("message", "丝绸之路确认信息编辑成功") ;
		}
		return new ProjectController().showspList(fpid, spid, model);
	}
	
    @RequestMapping(value="/{stepid}/xxqrdownload",method=RequestMethod.GET)
    public void download(@PathVariable String stepid,HttpServletResponse res,HttpServletRequest req) throws IOException {
        OutputStream os = res.getOutputStream();
        try {
            res.reset();
            res.setHeader("Content-Disposition", "attachment; filename="+stepid+"xxqr.xlsx");
            res.setContentType("application/octet-stream; charset=utf-8");
            String realPath = req.getSession().getServletContext().getRealPath("/attach/skilroad/xxqr/") ;
            SilkroadProjectService sps = new SilkroadProjectServiceImpl() ;
            os.write(FileUtils.readFileToByteArray(sps.downloadXxqr(stepid, realPath+stepid+"xxqr.xlsx")));
            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

}
